<?php
/**
 * Model_Posts 日志模型
 *
 * @package Movoin Blog
 * @subpackage Model
 *
 */

class Model_Posts
{

	/**
	 * Table_Posts
	 *
	 * @var object
	 */
    var $tblPosts;

    /**
     * Model_Posts
     *
     * @return Model_Posts
     */
    function Model_Posts()
    {
        $this->tblPosts = & FLEA::getSingleton('Table_Posts');
    }

    /**
     * 获取日志记录
     *
     * @param int $post_id
     * @param string $fields
     * @param bool $links
     * @param string $orderby
     *
     * @return array
     */
    function getPost($post_id=null, $fields='*', $links=false, $orderby='created DESC, post_id DESC'){
    }

    /**
     * 获取日期列表
     *
     * @param bool $opt
     * @return array
     */
    function getArchives(){
        $return = $archives = $this->tblPosts->findBySql('SELECT DISTINCT `post_date`,count(post_id) as `posts` FROM `mo_posts` GROUP BY `post_date` ORDER BY `cate_id` DESC');
        if($archives) {
            $return = array();
	        foreach($archives as $archive){
	            $return[] = array(
	                        'name' => $archive['post_date'],
	                        'value' => $archive['post_date'],
	                        'posts' => $archive['posts']
	                    );
	        }
        }
        return $return;
    }

    /**
     * 查询日志总数
     *
     * @param string $status
     * @return int
     */
    function getCount($status=null){
    	$where = null;
    	if($status){ $where = array(array('status',$status)); }
    	return $this->tblPosts->findCount($where);
    }

    /**
     * 发布/更新 日志
     *
     * @param array $data
     *
     * @return string
     */
    function savePost(&$data){
    	$msg = 'Failed';
    	$data['post_date'] = date('F Y');
    	$data['permanent_link'] = str_replace(trim($data['slug']), ' ', '-') . '.html';
        if ( $this->tblPosts->create(&$data) ){
            $msg = 'Success';
        }
        return $msg;
    }

    /**
     * 删除日志
     *
     * @param int|string $posts
     */
    function removePost($posts){
    }

    /**
     * 更新点击
     *
     * @param int $post_id
     */
    function _updateClick($post_id){
    }

    /**
     * 获取表实例
     *
     * @return object
     */
    function &getTable()
    {
        return $this->tblPosts;
    }

}
